package com.wxg.apache.basic;

import org.apache.http.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

/**
 * From : http://hc.apache.org/httpcomponents-core-ga/tutorial/html/fundamentals.html
 * date : 2018年07月11日
 */
public class BasicOperations {

    @Test
    public void testHttpRequestMessage(){
        HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);

        Assert.assertEquals("GET", request.getRequestLine().getMethod());
        Assert.assertEquals("/", request.getRequestLine().getUri());
        Assert.assertEquals("HTTP/1.1", request.getProtocolVersion().toString());
        Assert.assertEquals("GET / HTTP/1.1", request.getRequestLine().toString());
    }

    @Test
    public void testHttpResponseMessage(){
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
                HttpStatus.SC_OK, "OK");

        Assert.assertEquals("HTTP/1.1", response.getProtocolVersion().toString());
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
        Assert.assertEquals("HTTP/1.1 200 OK", response.getStatusLine().toString());
    }

    @Test
    public void testResponseAddHeader(){
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
                HttpStatus.SC_OK, "OK");
        response.addHeader("Set-Cookie",
                "c1=a; path=/; domain=localhost");
        response.addHeader("Set-Cookie",
                "c2=b; path=\"/\", c3=c, domain=\"localhost\"");
        Header h1 = response.getFirstHeader("Set-Cookie");
        Assert.assertEquals("Set-Cookie: c1=a; path=/; domain=localhost", h1.toString());

        Header h2 = response.getLastHeader("Set-Cookie");
        Assert.assertEquals("Set-Cookie: c2=b; path=\"/\", c3=c, domain=\"localhost\"", h2.toString());

        Header[] hs = response.getHeaders("Set-Cookie");
        Assert.assertEquals(2, hs.length);
    }

    @Test
    public void testHeaderIterator(){
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
                HttpStatus.SC_OK, "OK");
        response.addHeader("Set-Cookie",
                "c1=a; path=/; domain=localhost");
        response.addHeader("Set-Cookie",
                "c2=b; path=\"/\", c3=c, domain=\"localhost\"");

        HeaderIterator it = response.headerIterator("Set-Cookie");

        while (it.hasNext()){
            System.out.println(it.next());
        }
    }

    @Test
    public void testHeaderElementIterator(){
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
                HttpStatus.SC_OK, "OK");

        response.addHeader("Set-Cookie",
                "c1=a; path=/; domain=localhost");
        response.addHeader("Set-Cookie",
                "c2=b; path=\"/\", c3=c, domain=\"localhost\"");

        HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Set-Cookie"));

        while (it.hasNext()){
            HeaderElement elem = it.nextElement();
            System.out.println(elem.getName() + " = " + elem.getValue());
            NameValuePair[] params = elem.getParameters();
            for (int i=0;i < params.length; i++) {
                System.out.println(" " + params[i]);
            }
        }
    }


    @Test
    public void testStringEntity() throws IOException {
        StringEntity myEntity = new StringEntity("important message",
                Consts.UTF_8);

        Assert.assertEquals("Content-Type: text/plain; charset=UTF-8",
                myEntity.getContentType().toString());
        Assert.assertEquals(17, myEntity.getContentLength());
        Assert.assertEquals("important message",
                EntityUtils.toString(myEntity));
        Assert.assertEquals(17,
                EntityUtils.toByteArray(myEntity).length);
    }







}
